<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1,
        #box2 {
            width: 100px;
            height: 100px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <!-- <div id="box1"></div>
    <div id="box2"></div>

    <script>
        function turnred() {
            var self = this;
            setTimeout(function () {
                self.style.backgroundColor = 'red';
                console.log('1');
            }, 2000);
        }

        var box1 = document.getElementById('box1');
        var box2 = document.getElementById('box2');
        box1.onclick = turnred;
        box2.onclick = turnred;

    </script> -->
    <div id="box1"></div>
    <div id="box2"></div>

    <script>
        function turnred() {
            var self = this;
            setTimeout(function () {
                self.style.backgroundColor = 'red';
                console.log('1');
            }, 2000);
        }

        var box1 = document.getElementById('box1');
        var box2 = document.getElementById('box2');
        box1.onclick = turnred;
        box2.onclick = turnred;
    </script>

</body>

</html>